<%@codepage="65001"%>
<%session.codepage="65001"%>
<!-- #include virtual=/inc/dbConn.asp -->
<!-- #include virtual=/inc/function.asp -->
<!-- #include virtual=/inc/action.header.inc.asp -->

<%
	'## 파라미터 변수 선언부
	code		= Request("code")
	stdField		= Request("field")
	stdYear		= Request("year")
	stdType		= Request("type")
	stdSection	= Request("section")

	SQLTitle = "-- ■■■■ 통계 관리 그래프 ■■■■"
	
	'## 검색조건에 따른 조건절 만들기 
	If stdType <> "" Then 
		addSql = " AND A.type='" & stdType & "' "
	End If 

	If code = "" Then 
		Response.write "{   ""label"": ""reset""	}" & vbcrlf
		Response.end
	End If 

	If stdSection = "badRate" And (stdType = "10" Or stdType = "20") Then 
		stdCount = "CASE WHEN (SUM(A.goodQty)+SUM(A.badQty)) > 0 THEN ROUND(SUM(A.badQty)/(SUM(A.goodQty)+SUM(A.badQty))*100,2) ELSE 0 END "
		underPoint = 2
	Else
		stdCount = "SUM(A.goodQty)"
		underPoint = 0
	End If 


	SQL = "SELECT  " & stdField & " prdGrp, [" & stdYear & "-01] M01, [" & stdYear & "-02] M02, [" & stdYear & "-03] M03, [" & stdYear & "-04] M04, [" & stdYear & "-05] M05, [" & stdYear & "-06] M06, " & _
			"	[" & stdYear & "-07] M07, [" & stdYear & "-08] M08, [" & stdYear & "-09] M09, [" & stdYear & "-10] M10,[" & stdYear & "-11] M11, [" & stdYear & "-11] M12 " & _
			"	FROM ( " & _
			"		SELECT C." & stdField & ", LEFT(A.workDt,7) wDt, " & stdCount & " Qty " & _
			"		FROM tblOrderResult A " & _
			"			LEFT JOIN tblOrder B ON A.lotYear=B.lotYear AND A.lotNo=B.lotNo AND A.lotSubNo=B.lotSubNo " & _
			"			LEFT JOIN tblProduct C ON B.prdCd=C.prdCd " & _
			"		WHERE A.flag='Y' AND B.flag<>'N' AND ISNULL(A.workDt,'')<>'' AND C.prdGrp<>'' " & addSql & _
			"		GROUP BY LEFT(A.workDt,7), C." & stdField & " " & _
			"		UNION ALL " & _
			"		SELECT 'all' as prdGrp, LEFT(A.workDt,7) wDt, " & stdCount & " Qty " & _
			"		FROM tblOrderResult A " & _
			"			LEFT JOIN tblOrder B ON A.lotYear=B.lotYear AND A.lotNo=B.lotNo AND A.lotSubNo=B.lotSubNo " & _
			"			LEFT JOIN tblProduct C ON B.prdCd=C.prdCd " & _
			"		WHERE A.flag='Y' AND B.flag<>'N' AND ISNULL(A.workDt,'')<>'' AND C.prdGrp<>'' " & addSql & _
			"		GROUP BY LEFT(A.workDt,7)	" & _
			"	) K " & _
			"	PIVOT ( SUM(QTY) FOR wDt IN ([" & stdYear & "-01] ,[" & stdYear & "-02], [" & stdYear & "-03],[" & stdYear & "-04] ,[" & stdYear & "-05],[" & stdYear & "-06], " & _
			"	[" & stdYear & "-07] ,[" & stdYear & "-08], [" & stdYear & "-09],[" & stdYear & "-10] ,[" & stdYear & "-11], [" & stdYear & "-12] )) pvt " & _
			"	WHERE " & stdField & "='" & code & "' " & _
			"	ORDER BY prdGrp	"
'Response.write sql
	If xMode="debug" Then 
		Call showDebug(SQLTitle,SQL) 
	End If 

	Set Rs=Server.CreateObject("ADODB.RecordSet")
	Rs.open SQL, DbCon, 1
	
	Response.write "{" & vbcrlf

	If Rs.eof Or Rs.bof Then 
	Else 
		Do Until Rs.eof Or Rs.bof
			m01 = "0"
			m02 = "0"
			m03 = "0"
			m04 = "0"
			m05 = "0"
			m06 = "0"
			m07 = "0"
			m08 = "0"
			m09 = "0"
			m10 = "0"
			m11 = "0"
			m12 = "0"
			total = "1"
			If rs("m01") <> "" Then m01 = rs("m01") End If
			If rs("m02") <> "" Then m02 = rs("m02") End If
			If rs("m03") <> "" Then m03 = rs("m03") End If
			If rs("m04") <> "" Then m04 = rs("m04") End If
			If rs("m05") <> "" Then m05 = rs("m05") End If
			If rs("m06") <> "" Then m06 = rs("m06") End If
			If rs("m07") <> "" Then m07 = rs("m07") End If
			If rs("m08") <> "" Then m08 = rs("m08") End If
			If rs("m09") <> "" Then m09 = rs("m09") End If
			If rs("m10") <> "" Then m10 = rs("m10") End If
			If rs("m11") <> "" Then m11 = rs("m11") End If
			If rs("m12") <> "" Then m12 = rs("m12") End If
		
			Response.write "   ""label"": """ & rs("prdGrp") & """, " & vbcrlf
			Response.write 	"	""data"": [[1, " & m01 & "], [2, " & m02 & "], [3, " & m03 & "], [4, " & m04 & "], [5, " & m05 & "], [6, " & m06 & "],"
			Response.write "[7, " & m07 & "], [8, " & m08 & "], [9, " & m09 & "], [10, " & m10 & "], [11, " & m11 & "], [12, " & m12 & "]]" & vbcrlf

		Rs.movenext
		Loop 
	End If
	Response.write "}" & vbcrlf
'	Call rsWithXML("Save OK!")
%>